//
//  MainView.m
//  OASystem
//
//  Created by macos on 14-4-15.
//  Copyright (c) 2014年 macos. All rights reserved.
//

#import "MainView.h"

@interface MainView ()

@end

@implementation MainView
@synthesize nameStr,passwordStr;
@synthesize documentFlag;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"title_bg.png"] forBarMetrics:UIBarMetricsDefault];
}

//注销操作
- (void)logout
{
//    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"temp"];
//    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"USER_NAME"];
//    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"USER_PASSWORD2"];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];
    
    mainScr.contentSize = CGSizeMake(320, 480);
    
    //初始化UINavigation
    self.navigationItem.titleView = [[UIView alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    titleLabel.text = @"首页";
    titleLabel.font = HEIGHT_FONT(18.0f);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    [self.navigationItem.titleView addSubview:titleLabel];
    
    //添加导航又边用户信息
    //初始化UINavigation左按钮
    UIButton *helloButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 80, 24)];
    [helloButton setTitle:[NSString stringWithFormat:@"你好，%@",USER_NICK_NAME] forState:UIControlStateNormal];
    helloButton.titleLabel.font = HEIGHT_FONT(13.0f);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:helloButton];
    
    //初始化UINavigation左按钮
    UIButton *naviButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 45, 24)];
    [naviButton setTitle:@"注销" forState:UIControlStateNormal];
    naviButton.titleLabel.font = HEIGHT_FONT(13.0f);
    [naviButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_n.png"] forState:UIControlStateNormal];
    [naviButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_p.png"] forState:UIControlStateHighlighted];
    [naviButton addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:naviButton];
    
    
    if(documentFlag != 1)
    {
        documentButton.hidden = YES;
        documentLabel.hidden = YES;
    }
}

//选择子菜单的操作
- (IBAction)selectItremAction:(UIButton *)sender
{
    [mailButton setBackgroundImage:[UIImage imageNamed:@"gwyj.png"] forState:UIControlStateNormal];
    [documentButton setBackgroundImage:[UIImage imageNamed:@"dsgw.png"] forState:UIControlStateNormal];
    [thingsButton setBackgroundImage:[UIImage imageNamed:@"dbsy.png"] forState:UIControlStateNormal];
    [boardButton setBackgroundImage:[UIImage imageNamed:@"ggl.png"] forState:UIControlStateNormal];
    [addressButton setBackgroundImage:[UIImage imageNamed:@"txl.png"] forState:UIControlStateNormal];
    [studyButton setBackgroundImage:[UIImage imageNamed:@"xxyd.png"] forState:UIControlStateNormal];

    switch (sender.tag)
    {
        case 0:
        {
            //公务邮箱
            [mailButton setBackgroundImage:[UIImage imageNamed:@"gwyj_hover.png"] forState:UIControlStateNormal];
            
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"LOAD" object:nil];

            OfficalMail *instance = [[OfficalMail alloc] init];
            instance.noReadStr = @"0";
            [self.navigationController pushViewController:instance animated:YES];
            
//            //获取收件箱未读邮件
//            [[SendHttpOperation shareSendHttpOperation] GetNoReadCount:@"GetNoReadCount.ashx"
//                                                           BoxTypeGuid:@"100000"
//                                                         ownerUserGuid:USER_OWNER_GUDID];
//            [SendHttpOperation shareSendHttpOperation].getNoReadCountDelegate = self;
        }
            break;
        case 1:
        {
            //通讯录
//            [addressButton setBackgroundImage:[UIImage imageNamed:@"txl_hover.png"] forState:UIControlStateNormal];
        }
            break;
        case 2:
        {
            //待办事宜
            [thingsButton setBackgroundImage:[UIImage imageNamed:@"dbsy_hover.png"] forState:UIControlStateNormal];
            
            [self boolLogin:0];
        }
            break;
        case 3:
        {
            //待收公文
            [documentButton setBackgroundImage:[UIImage imageNamed:@"dsgw_hover.png"] forState:UIControlStateNormal];
            
            [self boolLogin:1];
        }
            break;
        case 4:
        {
            //公告栏
            [boardButton setBackgroundImage:[UIImage imageNamed:@"ggl_hover.png"] forState:UIControlStateNormal];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"LOAD" object:nil];
            
            //获取公告栏列表
            [[SendHttpOperation shareSendHttpOperation] GetWebInfoList:@"GetWebInfoList.ashx"
                                                                   Cur:@"0"
                                                                  Rows:@"10"];
            [SendHttpOperation shareSendHttpOperation].getWebInfoListDelegate = self;
        }
            break;
        case 5:
        {
            //学习园地
            [studyButton setBackgroundImage:[UIImage imageNamed:@"xxyd_hover.png"] forState:UIControlStateNormal];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"LOAD" object:nil];
            
            //获取公告栏列表
            [[SendHttpOperation shareSendHttpOperation] studyGarde:@"GetWebInfoListBy010.ashx"
                                                               Cur:@"0"
                                                              Rows:@"10"];
            [SendHttpOperation shareSendHttpOperation].getStudyGardeDelegate = self;
        }
            break;
        default:
            break;
    }
}

- (void)boolLogin:(int)flag
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"LOAD" object:nil];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *tempDic = [[NSDictionary alloc] init];
        tempDic = [[SendHttpOperation shareSendHttpOperation] login_do:nameStr passWord:passwordStr];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"REMOVE" object:nil];
            if ([[tempDic objectForKey:@"errcode"] isEqualToString:@"0"])
            {
                if(flag == 0)
                {
                    WaitHandleList *instance = [[WaitHandleList alloc] init];
                    [self.navigationController pushViewController:instance animated:YES];
                }
                else
                {
                    WaitDocumentList *instance = [[WaitDocumentList alloc] init];
                    [self.navigationController pushViewController:instance animated:YES];
                }
            }
            else
                [[NSNotificationCenter defaultCenter] postNotificationName:@"TIP" object:@{@"msg":@"用户验证失败，请重新尝试！"}];
        });
    });
}

//获取已读回调
//- (void)passNoReadCount:(NSDictionary *)NoReadCountDic
//{
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"REMOVE" object:nil];
//    
//    OfficalMail *instance = [[OfficalMail alloc] init];
//    instance.noReadStr = [NSString stringWithFormat:@"%@",[[NoReadCountDic objectForKey:@"result"] objectForKey:@"value"]];
//    [self.navigationController pushViewController:instance animated:YES];
//}

//获取公告列表回调
- (void)passGetWebInfoList:(NSDictionary *)loginDic
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"REMOVE" object:nil];
    if([[loginDic allKeys] containsObject:@"result"])
    {
        NoticeBoardView *instance = [[NoticeBoardView alloc] init];
        instance.passListDic = [NSMutableDictionary dictionaryWithDictionary:[[loginDic objectForKey:@"result"] objectForKey:@"list"]];
        [self.navigationController pushViewController:instance animated:YES];
    }
    else
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TIP" object:@{@"msg":@"网络获取数据失败，请重新尝试！"}];
}

//获取学习园地回调
- (void)passGetStudyGarde:(NSDictionary *)getStudyGardeDic
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"REMOVE" object:nil];

    if([[getStudyGardeDic allKeys] containsObject:@"result"])
    {
        StudyGardeMain *instance = [[StudyGardeMain alloc] init];
        instance.passStudyDic = [NSMutableDictionary dictionaryWithDictionary:[[getStudyGardeDic objectForKey:@"result"] objectForKey:@"list"]];
        [self.navigationController pushViewController:instance animated:YES];
    }
    else
        [[NSNotificationCenter defaultCenter] postNotificationName:@"TIP" object:@{@"msg":@"网络获取数据失败，请重新尝试！"}];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
